package com.elm.mapper;

import com.elm.po.Cart;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Result;

import java.util.List;

@Mapper
public interface CartMapper {
	@Insert("INSERT INTO cart (id, business_id, food_id, quantity,customer_id) " +
            "VALUES (#{id}, #{businessId}, #{foodId}, #{quantity}, #{customerId})")
    @Options(useGeneratedKeys = true, keyProperty = "id") // 关键：获取自增ID并赋值给cart对象的id字段
    int insertCart(Cart cart);
    
	@Update("Update cart set quantity=#{quantity} where customer_id=#{customerId} and business_id=#{businessId} and food_id=#{foodId}")
    int updateCart(Cart cart);
    
	class CartSqlProvider {
        public String deleteCart(Cart cart) {
            // 基础SQL，必传userId和businessId
            StringBuilder sql = new StringBuilder("DELETE FROM cart WHERE customer_id = #{customerId} AND business_id = #{businessId}");
            
            // 若foodId不为null，则添加食品ID条件
            if (cart.getFoodId() != 0) { // 假设未设置时默认值为0
                sql.append(" AND food_id = #{foodId}");
            }
            
            return sql.toString();
        }
    }
	@DeleteProvider(type = CartSqlProvider.class, method = "deleteCart")
    int removeCart(Cart cart);

    @Select("SELECT * FROM cart WHERE id = #{id}")
    Cart getCartById(int id);

    @Select("SELECT SUM(c.quantity * f.food_price) AS totalMoney \r\n"
    		+ "FROM cart c\r\n"
    		+ "JOIN food f ON c.food_id = f.id \r\n"
    		+ "WHERE c.customer_id = #{customerId} AND c.business_id = #{businessId}")
	Double getTotalByUserIdByBusinessId(int customerId, int businessId);

    @Select("select * from cart where business_id=#{businessId} And customer_id=#{userId}")
   	List<Cart> getCartByBusiness(int businessId, int userId);

    @Select("select * from cart where food_id=#{foodId} And customer_id=#{userId}")
	Cart getCartByFoodByUser(int foodId, int userId);
    
    
    
    @Select("SELECT c.*, " +
            "f.id AS ffid, " +
            "f.food_name AS fffoodname, " +
            "f.food_explain AS fffoodexplain, " +
            "f.food_img AS fffoodimg, " +
            "f.food_price AS fffoodprice, " +
            "f.status AS ffstatus, " +
            "f.business_id AS ffbusinessid " +
            "FROM cart c " +
            "LEFT JOIN food f ON c.food_id = f.id " +
            "WHERE c.business_id = #{businessId} AND c.customer_id = #{userId}")
    @Results({
        // Cart 自身字段映射
        @Result(column = "id", property = "id"),
        @Result(column = "creator", property = "creator"),
        @Result(column = "isDeleted", property = "isDeleted"),
        @Result(column = "updater", property = "updater"),
        @Result(column = "quantity", property = "quantity"),
        @Result(column = "business_id", property = "businessId"),
        @Result(column = "customer_id", property = "customerId"),
        @Result(column = "food_id", property = "foodId"),
        
        // Food 对象映射
        @Result(column = "ffid", property = "food.id"),
        @Result(column = "fffoodname", property = "food.foodName"),
        @Result(column = "fffoodexplain", property = "food.foodExplain"),
        @Result(column = "fffoodimg", property = "food.foodImg"),
        @Result(column = "fffoodprice", property = "food.foodPrice"),
        @Result(column = "ffstatus", property = "food.status"),
        @Result(column = "ffbusinessid", property = "food.businessId")
    })
    List<Cart> getCartWithFoodByBusiness(int businessId, int userId);
}
